using System.Text.RegularExpressions;

namespace RenameX.Rules
{
    public class ReplaceRule : RuleBase
    {
        public override string Name => "替换";

        private string _findText = string.Empty;
        public string FindText { get => _findText; set => SetProperty(ref _findText, value); }

        private string _replaceText = string.Empty;
        public string ReplaceText { get => _replaceText; set => SetProperty(ref _replaceText, value); }

        private bool _useRegex;
        public bool UseRegex { get => _useRegex; set => SetProperty(ref _useRegex, value); }

        private bool _ignoreCase = true;
        public bool IgnoreCase { get => _ignoreCase; set => SetProperty(ref _ignoreCase, value); }

        public override string Apply(string originalName)
        {
            if (string.IsNullOrEmpty(FindText)) return originalName;

            if (UseRegex)
            {
                var options = IgnoreCase ? RegexOptions.IgnoreCase : RegexOptions.None;
                return Regex.Replace(originalName, FindText, ReplaceText ?? string.Empty, options);
            }

            var comparison = IgnoreCase ? System.StringComparison.OrdinalIgnoreCase : System.StringComparison.Ordinal;
            return originalName.Replace(FindText, ReplaceText ?? string.Empty, comparison);
        }
    }
}


